from app import db_main
from app.models.user_model import User
from app.models.analytics.user_analytics_model import UserAnalytics
# from app import db_main
# from app.models.user_model import User
# from app.models.analytics.user_analytics_model import UserAnalytics

class UserService:
    @staticmethod
    def create_user(data):
        new_user = User(
            username=data['username'],
            email=data['email']
        )
        db_main.session.add(new_user)
        db_main.session.commit()

        # 同时在分析数据库中创建记录
        # new_analytics = UserAnalytics(
        #     user_id=new_user.id,
        #     login_count=0
        # )
        # db_analytics.session.add(new_analytics)
        # db_analytics.session.commit()

        return new_user

    @staticmethod
    def get_all_users():
        """获取所有用户"""
        try:
            users = User.query.all()  # 假设你的User模型是SQLAlchemy模型
            return users
        except Exception as e:
            # 可以记录日志
            print(f"获取用户列表失败: {str(e)}")
            raise  # 重新抛出异常，由路由处理
